<?php 
/****

	If you want the best of the world,
give the world your best anytime！
            				——不二
                							
****/

defined('ACCESS')||exit('Forbidden!');
//栏目相关控制器
class CategoryAction extends Action {
	protected $cat;
	public function __construct(){
		parent::__construct();
		$this->cat = new CategoryModel();
	}

	//后台首页
	public function index(){
		$this->display('index.html');
	}

	//栏目添加
	public function add(){
		if(empty($_POST)){
			$this->display('cat_add.html');
		}else{
			$res = $this->cat->insert($_POST);
			if($res){
				$this->success('栏目添加成功','index.php?group=Admin&module=Category&action=lists');
			}else {
				$this->failure('栏目添加失败','index.php?group=Admin&module=Category&action=add');
			}
		}
	}

	//栏目列表
	public function lists(){
		$data = $this->cat->select();
		$this->assign('data',$data);
		$this->display('cat_list.html');
	}

	//栏目删除
	public function del(){
		$id = $this->I('id');
		$data = $this->cat->find($id);
		if(!$data){
			$this->failure('栏目不存在','index.php?group=Admin&module=Category&action=lists');
		}
		if($data['num'] != 0){
			$this->failure('栏目下有文章','index.php?group=Admin&module=Category&action=lists');
		}
		$res = $this->cat->delete($id);
		if(!$res){
			$this->failure('删除失败','index.php?group=Admin&module=Category&action=lists');
		}else {
			header('Location:index.php?group=Admin&module=Category&action=lists');
		}
	}

	//栏目修改
	public function edit(){
		$id = $this->I('id');
		$data = $this->cat->find($id);
		if(!$data){
			$this->failure('栏目不存在','index.php?group=Admin&module=Category&action=lists');
		}
		if(empty($_POST)){
			//取出默认值
			$this->assign('data',$data);
			$this->display('cat_edit.html');
		}else {
			$_POST['cat_id'] = $id;
			$res = $this->cat->update($_POST);
			if(!$res){
				$this->failure('修改失败','index.php?group=Admin&module=Category&action=lists');
			}else {
				$this->success('修改成功','index.php?group=Admin&module=Category&action=lists');
			}
		}
	}
}

